package org.openintents.droidcon.cloud.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openintents.droidcon.cloud.model.Panel;
import org.openintents.droidcon.cloud.model.Room;
import org.openintents.droidcon.cloud.model.Schedule;
import org.openintents.droidcon.cloud.model.Slot;
import org.openintents.droidcon.cloud.mvc.PanelForm;
import org.openintents.droidcon.cloud.mvc.RoomForm;
import org.openintents.droidcon.cloud.mvc.ScheduleForm;
import org.openintents.droidcon.cloud.mvc.ScheduleList;
import org.openintents.droidcon.cloud.mvc.SlotForm;

@SuppressWarnings("serial")
public class ScheduleServlet extends HttpServlet {

	private String action;
	private int id;
	private long roomId;
	private long panelId;
	private long slotId;
	private String name;
	private String location;
	private String date;
	private String alias;
	private String from;
	private String to;
	private String title;
	private String description;
	private String speaker;

	private HttpServletRequest req;
	private HttpServletResponse resp;
	private String msg;

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		init(req, resp);

		// delete schedule by id
		if (action.equals("delete") && id > 0) {
			Schedule deleteSchedule = Schedule.find(id);
			deleteSchedule.delete();
			id = 0;
		}
		
		// delete room by id
		if (action.equals("deleteRoom") && id > 0 && roomId > 0) {
			Schedule deleteSchedule = Schedule.find(id);
			Room deleteRoom = deleteSchedule.findRoom(roomId);
			deleteSchedule.getRooms().remove(deleteRoom);
			deleteSchedule.persist();
			id = 0;
			roomId = 0;
		}
		// delete panel by id
		if (action.equals("deletePanel") && id > 0 && panelId > 0) {
			Schedule deleteSchedule = Schedule.find(id);
			Panel deletePanel = deleteSchedule.findPanel(panelId);
			deleteSchedule.getRooms().remove(deletePanel);
			deleteSchedule.persist();
			id = 0;
		}
		// delete slot by id
		if (action.equals("deleteSlot") && id > 0 && panelId > 0 && slotId > 0) {
			Schedule deleteSchedule = Schedule.find(id);
			Panel deletePanel = deleteSchedule.findPanel(panelId);
			Slot deleteSlot = deletePanel.findSlot(slotId);
			deletePanel.getSlots().remove(deleteSlot);
			deleteSchedule.persist();
			id = 0;
		}
		

		displayContent();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		init(req, resp);

		msg = action + " ";
		
		// add a new schedule
		if (action.equals("add") && !name.equals("")) {
			Schedule schedule = new Schedule(name);
			schedule.setLocation(location);
			schedule.setDateString(date);
			schedule.persist();
		}

		// edit schedule by id - set name
		if (action.equals("edit") && id > 0 && name != null && !name.equals("")) {
			Schedule editSchedule = Schedule.find(id);
			editSchedule.setName(name);
			editSchedule.setLocation(location);
			editSchedule.setDateString(date);
			editSchedule.persist();
			editSchedule = null;
			id = 0;
		}
		
		// add a new room
		if (action.equals("addRoom") && id > 0 ) {
			Schedule editSchedule = Schedule.find(id);
			editSchedule.getRooms().add(new Room(name, alias));
			editSchedule.persist();
			editSchedule = null;
			id = 0;
			msg += "Added room " + name + "/" + alias + "<br/>";
		}
		
		// edit room by id - set name and alias
		if (action.equals("editRoom") && id > 0 && roomId > 0 && name != null && !name.equals("") && alias != null && !alias.equals("")) {
			Schedule editSchedule = Schedule.find(id);
			Room editRoom = editSchedule.findRoom(roomId);
			editRoom.setName(name);
			editRoom.setAlias(alias);
			editSchedule.persist();
			editSchedule = null;
			id = 0;
			roomId = 0;		
		}

		// add a new panel
		if (action.equals("addPanel") && id > 0 ) {
			Schedule editSchedule = Schedule.find(id);
			editSchedule.getPanels().add(new Panel(from, to));
			editSchedule.persist();
			editSchedule = null;
			id = 0;
			msg += "Added panel " + name + "/" + alias + "<br/>";
		}
		
		// edit panel by id - set from, to
		if (action.equals("editPanel") && id > 0 && panelId > 0 && from != null && !from.equals("") && to != null && !to.equals("")) {
			Schedule editSchedule = Schedule.find(id);
			Panel editPanel = editSchedule.findPanel(panelId);
			editPanel.setFromTo(from, to);			
			editSchedule.persist();
			editSchedule = null;
			id = 0;
			roomId = 0;		
		}
		
		// add a new slot
		if (action.equals("addSlot") && id > 0 && panelId > 0) {
			Schedule editSchedule = Schedule.find(id);
			Panel editPanel = editSchedule.findPanel(panelId);
			editPanel.getSlots().add(new Slot(title, description, speaker, alias));
			editSchedule.persist();
			editSchedule = null;
			id = 0;
			msg += "Added slot " + title + "<br/>";
		}
		
		// edit slot by id - set title, description, speaker, alias
		if (action.equals("editSlot") && id > 0 && panelId > 0 && slotId > 0) {
			Schedule editSchedule = Schedule.find(id);
			Panel editPanel = editSchedule.findPanel(panelId);
			Slot editSlot = editPanel.findSlot(slotId);
			editSlot.setTitle(title);
			editSlot.setDescription(description);
			editSlot.setSpeaker(speaker);
			editSlot.setRoomAlias(alias);
			editSchedule.persist();
			editSchedule = null;
			id = 0;
			roomId = 0;		
		}
		displayContent();
	}

	/**
	 * Sets the content type and inits all given parameters
	 * 
	 * @param _req
	 * @param _resp
	 * @throws IOException
	 */
	private void init(HttpServletRequest _req, HttpServletResponse _resp)
			throws IOException {

		req = _req;
		resp = _resp;

		resp.setContentType("text/html");

		// set name
		name = "";
		if (req.getParameter("name") != null
				&& !req.getParameter("name").equals("")) {
			name = req.getParameter("name");
		}
		
		// set alias
		alias = "";
		if (req.getParameter("alias") != null
				&& !req.getParameter("alias").equals("")) {
			alias = req.getParameter("alias");
		}
		
		// set from
		from = "";
		if (req.getParameter("from") != null
				&& !req.getParameter("from").equals("")) {
			from = req.getParameter("from");
		}
		
		// set to
		to = "";
		if (req.getParameter("to") != null
				&& !req.getParameter("to").equals("")) {
			to = req.getParameter("to");
		}
		
		// set title
		title = "";
		if (req.getParameter("title") != null
				&& !req.getParameter("title").equals("")) {
			title = req.getParameter("title");
		}
		
		// set date
		date = "";
		if (req.getParameter("date") != null
				&& !req.getParameter("date").equals("")) {
			date = req.getParameter("date");
		}
		
		// set title
		location = "";
		if (req.getParameter("location") != null
				&& !req.getParameter("location").equals("")) {
			location = req.getParameter("location");
		}
		
		// set description
		description = "";
		if (req.getParameter("description") != null
				&& !req.getParameter("description").equals("")) {
			description = req.getParameter("description");
		}
		// set speaker
		speaker = "";
		if (req.getParameter("speaker") != null
				&& !req.getParameter("speaker").equals("")) {
			speaker = req.getParameter("speaker");
		}

		// set id
		id = 0;
		if (req.getParameter("id") != null) {
			id = Integer.parseInt(req.getParameter("id"));
		}
		
		// set roomId
		roomId = 0L;
		if (req.getParameter("roomId") != null) {
			roomId = Long.parseLong(req.getParameter("roomId"));
		}
		
		// set panelId
		panelId = 0L;
		if (req.getParameter("panelId") != null) {
			panelId = Long.parseLong(req.getParameter("panelId"));
		}
		
		// set slotId
		slotId = 0L;
		if (req.getParameter("slotId") != null) {
			slotId = Long.parseLong(req.getParameter("slotId"));
		}

		// set action
		action = "";
		if (req.getParameter("action") != null
				&& !req.getParameter("action").equals("")) {
			action = req.getParameter("action");
		}
	}

	private void displayContent() throws IOException {
		// create XHTML page
		WebPage page = new WebPage("Schedule Database");
		
		page.addContent(msg);
		
		page.addContent("<h1>Schedule Database</h1>");
		// add list with schedules
		List<Schedule> schedules = Schedule.findAll();
		ScheduleList list = new ScheduleList(schedules);
		page.addContent(list.display());
		// add form to add and edit schedules
		Schedule editSchedule = null;
		Room editRoom = null;
		Panel editPanel = null;
		Slot editSlot = null;
		if (id > 0) {
			editSchedule = Schedule.find(id);
			if (roomId >0){
				editRoom = editSchedule.findRoom(roomId);
			}
			if (panelId > 0){
				editPanel = editSchedule.findPanel(panelId);
				
				if (slotId > 0){
					editSlot = editPanel.findSlot(slotId);
				}
			}
		}
		ScheduleForm form = new ScheduleForm(editSchedule);
		page.addContent(form.display());
		
		
		RoomForm roomForm = new RoomForm (editSchedule, editRoom );
		page.addContent(roomForm.display());
		
		PanelForm panelForm = new PanelForm (editSchedule, editPanel );
		page.addContent(panelForm.display());

		SlotForm slotForm = new SlotForm (editSchedule, editPanel, editSlot );
		page.addContent(slotForm.display());
		
		// print generated XHTML page
		PrintWriter out = resp.getWriter();
		out.println(page.display());
	}
}
